<?php
class LinksAction extends Action {
	public function index(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		$m=M('Links');
		$data=$m->select();
		$this->assign('data',$data);
		$this->display();
	}
	
	public function create(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		$m=M('Links');
		$max=$m->max('id');
		$this->assign('max',$max+1);
		
		$ordering=$m->getField('ordering,title');
		$this->assign('ordering',$ordering);
		$this->display();
	}
	public function addlinks(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		if($this->isPost()){
			$m=M('Links');
			$data=$m->create();
			$data['url']='http://'.ltrim($data['url'],'http://');
			$data['logo']='';
			if($m->add($data)){
				$this->success('成功','index');
			}else{
				$this->error('失败');
			}
		}
	}
	
	public function edit(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		$m=M('Links');
		$where['id']=$_GET['id'];
		$data=$m->where($where)->select();
		$this->assign('data',$data[0]);
		$ordering=$m->getField('ordering,title');
		$this->assign('ordering',$ordering);
		$this->display('update');
	}
	public function delete(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		$m=M('Links');
		$where['id']=$_GET['id'];
		$m->where($where)->delete() or $this->error('删除失败');
		$this->success('删除成功');
	}
	public function update(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/login');
		}
		$m=M('Links');
		$m->create();
		$m->save() or $this->error('失败');
		$this->success('成功','index');
	}
}

?>